package com.zzyl.serve.service.impl;

import java.util.List;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanProjectVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingProjectPlanVo selectNursingPlanById(Long id) {
        // 1. 查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        // 2.创建对象整合护理计划和护理项目
        NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingProjectPlanVo);
        // 3. 根据护理计划id查询护理项目
        List<NursingProjectPlan> list = nursingProjectPlanMapper.selectById(id);
        nursingProjectPlanVo.setProjectPlans(list);
        return nursingProjectPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto) {
        // 1. 拷贝参数到实体
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        // 2. 设置创建时间
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        // 3. 新增护理计划
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        // 4. 新增护理项目
        int count = nursingProjectPlanMapper.addProjectPlan(dto.getProjectPlans(), nursingPlan.getId());
        return count;

    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        // 1. 拷贝参数到护理计划的类中
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0){
            nursingProjectPlanMapper.deleteNursingProjectPlanById(nursingPlan.getId());
            nursingProjectPlanMapper.addProjectPlan(dto.getProjectPlans(), nursingPlan.getId());
        }
        int count = nursingPlanMapper.updateNursingPlan(nursingPlan);
        return count;

    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Long id) {
        // 1. 查询护理计划的状态
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        // 2. 判断状态，启用状态1时不能删除
        if (nursingPlan.getStatus() == 1){
            return 0;
        }else {
            // 1. 删除护理项目
            nursingProjectPlanMapper.deleteNursingProjectPlanById(id);
            // 2. 删除护理计划
            int i = nursingPlanMapper.deleteNursingPlanById(id);
            return i > 0 ? 1 : 0;
        }
    }
}
